package com.ait.cms.controller;


import com.ait.cms.entity.UcenterMember;
import com.ait.cms.entity.vo.RegisterVo;
import com.ait.cms.service.UcenterMemberService;
import com.ait.commonutils.Jwt.JwtUtils;
import com.ait.commonutils.Msg;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-01
 */
@RestController
@RequestMapping("/cms/ucenterMember")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation("登录")
    @PostMapping("login")
    public Msg loginUser(@RequestBody UcenterMember member){
        String token = ucenterMemberService.login(member);
        return Msg.success().data("token",token);
    }

    @ApiOperation("注册")
    @PostMapping("register")
    public Msg registerUser(@RequestBody RegisterVo registerVo){
        boolean token = ucenterMemberService.register(registerVo);
        return Msg.success().data("token",token);
    }


    @ApiOperation("获取")
    @GetMapping("getMemberInfo")
    public Msg getMemberInfo(HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember byId = ucenterMemberService.getById(memberIdByJwtToken);
            return Msg.success().data("member",byId);
    }


    @GetMapping("getById")
    public Msg getById(String id){
        UcenterMember byId = ucenterMemberService.getById(id);
        return Msg.success().data("UcenterMember",byId);
    }

    //修改密码
    @PutMapping("modify")
    public Msg modify(@RequestBody RegisterVo registerVo){
        if (ucenterMemberService.modify(registerVo)) {
            return Msg.success();
        }
        return Msg.fail();
    }

    //修改个人信息
        @PutMapping("modifyUcenterMember")
    public Msg modifyUcenterMember(@RequestBody UcenterMember ucenterMember){
        if (ucenterMemberService.updateById(ucenterMember))
        {
            String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname());
            return Msg.success().data("token",jwtToken);
        }
        return Msg.fail();
    }
}

